<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once(OVF_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(OVF_FS_WEB_ROOT.'/app/_inc/funct.html.php');
require_once OVF_FS_WEB_ROOT.'/app/_inc/class.ovfForm.php';

$objSess->setReqSV('agenda', 'dataAge', date('d/m/Y'));
$objSess->setReqSV('agenda', 'agende_txt', $objSess->nome);
           
$sSql = "SELECT * FROM ovf_agenda_tipi_cont ORDER BY descr";
//dbg_($sSql);
$objDB->query($sSql,'selId');
$aTipiCont = array(0=>'-- seleziona tipo contatto');
while ($aX=$objDB->fetch('selId')) {
   $aTipiCont[$aX['id_tipo_cont']] = $aX['descr']." (".$aX['pallino'].")";
}

reqDef('listIdAge',$objSess->getSV('agenda','listAge'));
$sSql = "SELECT u.nome, u.cognome FROM ovf_utenti u "
   ." WHERE u.id_utente IN (".((reqVar('listIdAge'))?reqVar('listIdAge'):'0').") ";
//dbg_($sSql);
$objDB->query($sSql,'selUte');   
while($acX = $objDB->fetch('selUte')) {
   $aNomiAge[] = $acX['nome']." ".$acX['cognome'];
}
$sListNomiAge = implode(", ",$aNomiAge);

if (reqVar('azione') == 'salva') {
	$msgPag = confIns();
} 

$agendeTxt = $objSess->nome;
$sCmdR = '';
$sCmdR .= "&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:document.frm1.azione.value='salva';document.frm1.submit();\" >salva</a>\n";
$sCmdR .= cmdTorna('torna'); 

// testata sezione anagrafica
$oFormTestaSez = new ovfForm('testasez');
//dbg_($oForm);
$sSql = "SELECT ".$oFormTestaSez->getCampiSelect()
   ." FROM ovf_anagrafiche A WHERE id_anag = ".reqVarInt('idAnag');
$objDB->query($sSql);
if (!$oFormTestaSez->aCampiDB = $objDB->fetch()) {
   backOnErr("id. errato '".reqVar('idAnag')."'");
}
$oFormTestaSez->db2form();

$sHtmlAnag = "<table class=\"tab1\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"100%\" align='center'>";
$sHtmlAnag .= getHtmlRigaRif();
$sHtmlAnag .= $oFormTestaSez->getHtmlCampiView();
$sHtmlAnag .= "</table>";

$aOpz['sottotit'] = $sHtmlAnag;

testaHtml('Anagrafe','Registra contatto',$msgPag,'rcom',$aOpz);
?>
<script type="text/javascript">
<!--
function apriAgenModSelez() {
   window.open('<?=OVF_URL_ROOT?>/app/agen/agenModSelez.php?xsn=<?=XSN?>','gestTipoCont'
      ,'top=200,left=200,height=600,width=700,scrollbars=yes,resizable=yes');
}
function openWindTC() {
   window.open('<?=OVF_URL_ROOT?>/app/rcom/cont/gestTipoCont.php?xsn=<?=XSN?>','gestTipoCont'
      ,'top=200,left=200,height=600,width=700,scrollbars=yes,resizable=yes');
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left"><a href="javascript:openWindTC();" class='cmd1'>tipi contatto</a></td>
      <td class="cmd1" align="right"><?php echo $sCmdR;?></td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','...');?>
<?=getHtmlHidden('idAnag');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Contatto</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3' align='left'>
         <a class='lab1' href='javascript:apriAgenModSelez()'><?=_t("Agenti")?></a>
      </td>
      <td class="txt1" colspan='17' align='left'>
         <?php echo getHtmlHidden('listIdAge');?>
         <div id='listNomiAge'><?=$sListNomiAge?></div>
		</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3' align='left'>Oggetto</td>
      <td class="txt1" colspan='17' align='left'>
      	<?php echo getHtmlText('oggetto',100)?>
		</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3' align='left'>Data</td>
      <td class="txt1" colspan='3' align='left'>
      	<?php echo getHtmlDate('dataAge')?>
		</td>
      <td class="lab1" colspan='3' align='left'>Dove</td>
      <td class="txt1" colspan='11' align='left'>
      <?php echo getHtmlText('dove',40)?>
      </td>
   </tr>
   <tr>
      <td class="lab1" colspan='3' align='left'>Tipo</td>
      <td class="txt1" colspan='17' align='left'>
      <?php echo getHtmlSelect('id_tipo_cont',$aTipiCont)?>
      </td>
   </tr>
   <tr>
      <td class="lab1" colspan='3' align='left'>Nota</td>
      <td class="txt1" colspan='17' align='left'>
      <?php echo getHtmlTextArea('nota',100,10)?>
      </td>
   </tr>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right"><?php echo $sCmdR;?></td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function confIns() {
	global $objDB, $objSess;
	$msg = '';
	if (trim(reqVar('oggetto'))=='') {
		$msg .= "oggetto obbligatorio<br>";
	}
	if (reqVarStr2date('dataAge')===false) {
		$msg .= "data errata<br>";
	}
	if ($msg != '') {
		return $msg;
	}
	$anno = date('Y',reqVarStr2date('dataAge'));
	$mese = date('m',reqVarStr2date('dataAge'));
	$giorno = date('d',reqVarStr2date('dataAge'));
	$sSqlIns = "INSERT INTO ovf_agenda SET tipo_agenda = 'C' "
		.", anno = $anno, mese = $mese, giorno = $giorno"
		.", data_agenda = '".reqVarStr2YMD('dataAge')."'"
		.", oggetto = '".reqVarAS('oggetto')."'"
		.", dove = '".reqVarAS('dove')."'"
		.", pub_occ_pri = 'pub'"
		.", nota = '".reqVarAS('nota')."'"
		.", id_anag = ".reqVar('idAnag')
		.", id_tipo_cont = ".reqVarInt('id_tipo_cont')
      .", id_utente_ins = ".$objSess->idUtente;
	$objDB->exec($sSqlIns,'insRec');
	$newId = $objDB->lastInsertId('insRec');
	  
   $aX = explode(',',reqVar('listIdAge'));
   foreach($aX as $idX) {
      $sSql = "INSERT INTO ovf_agenda_utenti "
      ." SET id_agenda = $newId, id_utente = $idX";
      $objDB->exec($sSql);
   }
   
   header("Location: ".urlTorna("contatto registrato"));
	exit();
}
